package com.ygo.feihua.fragment;
import android.content.*;
import android.os.*;
import android.support.v4.app.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import cn.bmob.v3.*;
import cn.bmob.v3.datatype.*;
import cn.bmob.v3.listener.*;
import com.bumptech.glide.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.view.*;
import java.io.*;
import java.util.*;


import cn.bmob.v3.exception.*;
import com.ygo.feihua.util.*;
import com.yuyh.library.imgsel.*;
import com.yuyh.library.imgsel.ui.*;
import com.ygo.feihua.R;
import com.ygo.feihua.*;

public class FragmentZsZs extends Fragment
{

	private Gongju gj;

	private DialogUtils du;
	private ImageView fz_tb;
	private TextView fz_name,fz_js,fz_user,fz_size,fz_ztpath;
	private Button fz_ztcx;
	private ZhutiShangchuan zs;
	
	private MyUser mu;

	private LinearLayout llAdvertiseBoard;

	private LayoutInflater inflater;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_zs_zs,null);

		gj=Gongju.getdx(getActivity());
		du=DialogUtils.getdx(getActivity());
		
		zs=(ZhutiShangchuan) getActivity();
		fz_tb=(ImageView) v.findViewById(R.id.fz_tb);
		fz_js=(TextView) v.findViewById(R.id.fz_js);
		fz_user=(TextView) v.findViewById(R.id.fz_user);
		fz_size=(TextView) v.findViewById(R.id.fz_size);
		fz_name=(TextView) v.findViewById(R.id.fz_name);
		fz_ztpath=(TextView) v.findViewById(R.id.fz_ztpath);
		fz_ztcx=(Button) v.findViewById(R.id.fz_ztcx);
		llAdvertiseBoard = (LinearLayout)v.findViewById(R.id.llAdvertiseBoard);
		
		mu=BmobUser.getCurrentUser(MyUser.class);
		
		xscsh();
		
		Button zs_next=zs.sc_sc;
		zs_next.setText("上传");
		zs_next.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					if(new File(zs.path_zt).exists()){
						
					View[] v=du.dialogj1(false,"主题上传中","上传准备中");
						final TextView tv=(TextView) v[0];
					
					
						
						final String[] ss=new String[zs.zt_jt.size()];
						
						for(int i=0;i<zs.zt_jt.size();i++){
							
							ss[i]=zs.zt_jt.get(i);
						}
						
						
						final BmobFile bf=new BmobFile(new File(zs.path_tubiao));
						
						bf.uploadblock(new UploadFileListener(){


								@Override
								public void onProgress(Integer value) {
									// 返回的上传进度（百分比）
									tv.setText("图标上传中"+value+"%");
								}
								@Override
								public void done(BmobException p1)
								{
									if(p1==null){
										
										BmobFile.uploadBatch(ss, new UploadBatchListener() {

												@Override
												public void onSuccess(final List<BmobFile> files,List<String> urls) {
													//1、files-上传完成后的BmobFile集合，是为了方便大家对其上传后的数据进行操作，例如你可以将该文件保存到表中
													//2、urls-上传文件的完整url地址
													if(urls.size()==ss.length){//如果数量相等，则代表文件全部上传完成
														final BmobFile bf1=new BmobFile(new File(zs.path_zt));

														bf1.uploadblock(new UploadFileListener(){

																@Override
																public void done(BmobException p1)
																{
																	if(p1==null){
																		
																		Zhuti zt=new Zhuti();
																		zt.setztname(zs.zt_name);
																		zt.setztjie(zs.zt_js);
																		zt.setZtwj(bf1);
																		zt.setztuser(BmobUser.getCurrentUser(MyUser.class));
																		zt.setztxiaci(0);
																		zt.setZtsh(2);
																		zt.setztdx(zs.zt_size);
																		zt.setVercode(Gongju.ZHUTI_VERCODE);
																		zt.setzttb(bf);
																		zt.setZtjt(files);
																		zt.save(new SaveListener<String>(){

																				@Override
																				public void done(String p1, BmobException p2)
																				{
																					du.dis();
																					if(p2==null){
																						gj.show("上传成功");
																						getActivity().finish();
																					}else{
																						gj.show("主题上传失败,原因为"+p2);
																					}
																					
																					// TODO: Implement this method
																				}
																			});
																				
																		
																	}else{
																		du.dis();
																		gj.show("上传失败,原因为"+p1);
																	}
																	
																	// TODO: Implement this method
																}
																
																@Override
																public void onProgress(Integer value) {
																	// 返回的上传进度（百分比）
																	tv.setText("主题上传中"+value+"%");
																}
																
															});
																
													
														//do something
													}
												}

												@Override
												public void onError(int statuscode, String errormsg) {
													//ShowToast("错误码"+statuscode +",错误描述："+errormsg);
													du.dis();
													gj.show("上传失败,原因为"+errormsg);
													
												}

												@Override
												public void onProgress(int curIndex, int curPercent, int total,int totalPercent) {

													tv.setText("预览图上传中\n"+total+"/"+curIndex+"\n总进度:"+totalPercent+"%\n当前文件:"+curPercent+"%");
													//1、curIndex--表示当前第几个文件正在上传
													//2、curPercent--表示当前上传文件的进度值（百分比）
													//3、total--表示总的上传文件数
													//4、totalPercent--表示总的上传进度（百分比）
												}
											});


										/*
										 1、有多少个文件上传，onSuccess方法就会执行多少次;

										 2、通过onSuccess回调方法中的files或urls集合的大小与上传

										 */
										
										
									}else{
										du.dis();
										gj.show("上传失败,原因为"+p1);
									}
									// TODO: Implement this method
								}
							});
						
						
					}else{
						gj.show("主题文件不存在,请重新选择主题包");
					}
							

					// TODO: Implement this method
				}
			});
			return v;
			
	}

	private void xscsh()
	{
		fz_name.setText(zs.zt_name);
		fz_js.setText(zs.zt_js);
		fz_size.setText(gj.fileDx( zs.zt_size));
		fz_user.setText(mu.getUsername());
		fz_ztpath.setText(zs.path_zt);
		Glide.with(getActivity()).load(zs.path_tubiao).into(fz_tb);
		
		inflater = LayoutInflater.from(getActivity());
		
		llAdvertiseBoard.addView(new Advertisements(getActivity(), true, 3000, 3)
						.initView((List<Object>)zs.zt_jt));
								
		fz_ztcx.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					startActivityForResult(new Intent(getActivity(),NewWjxz.class).putExtra("lx",NewWjxz.TYPE_ZIP),0);
					
					
					// TODO: Implement this method
				}
			});
		fz_js.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					View[] v=du.dialoge(true,"主题介绍","");
					final EditText ed=(EditText) v[0];
					Button b1=(Button) v[1];
					ed.setText(zs.zt_js);
					b1.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								String js=ed.getText().toString().trim();
								if(js.equals("")){
									gj.show("主题介绍不能为空");
								}else{
									zs.zt_js=js;
									fz_js.setText(js);
									du.dis();
								}
								
								
								// TODO: Implement this method
							}
						});
					
					// TODO: Implement this method
				}
			});
			
		fz_tb.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					ISNav.getInstance().toListActivity(getActivity(), Gongju.getPicConfig(1), 233);
					
					// TODO: Implement this method
				}
			});
		
		// TODO: Implement this method
	}
	

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);

		if(resultCode==NewWjxz.TYPE_ZIP&&resultCode==NewWjxz.TYPE_ZIP){
			final String pa=data.getStringExtra("path");
	
			
			du.dialogj1(false,"","检查主题包中,请稍等");

			new Thread(new Runnable(){

					@Override
					public void run()
					{
						String ss=gj.ztJiancha(pa);
						Message me=new Message();
						me.obj=new String[]{ss,pa};
						me.what=0;
						han.sendMessage(me);
						// TODO: Implement this method
					}
				}).start();
			
		}
		
		if (resultCode == getActivity().RESULT_OK && requestCode == 233) {
			if (data != null) {
				ArrayList<String> photos = data.getStringArrayListExtra(ISListActivity.INTENT_RESULT);
				zs.path_tubiao=photos.get(0);
				//fz_tubiao.setImageBitmap(BitmapOptimization.getBitmap(photos.get(0), fz_tubiao.getWidth()));
				Glide.with(getActivity()).load(zs.path_tubiao).into(fz_tb);
			}
		}

	}
	Handler han=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case 0:
					du.dis();
					String[] s=(String[]) msg.obj;
					String ss=s[0];
					String pa=s[1];
					
					if(ss.equals("")){
						zs.path_zt=pa;
						zs.zt_size=(int)new File(pa).length();

						fz_ztpath.setText("已选择主题包"+pa);
						fz_size.setText(gj.fileDx( zs.zt_size));
					}else{

						View[] v=du.dialogt1(true,"该主题包格式错误",ss);
						Button b1=(Button) v[0];
						b1.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View p1)
								{
									du.dis();
									// TODO: Implement this method
								}
							});
					}
					break;
			}
			}
		};
		
	public void reGuanggao(View v){
			
			llAdvertiseBoard.removeViewAt(0);
			llAdvertiseBoard.addView(v);
			
			
		}
		
	
}
